package com.three.box.activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.three.box.R
import com.three.box.base.BaseActivity
import com.three.box.databinding.ActivityInOutLogBinding
import com.three.box.entity.ApiResult
import com.three.box.entity.BoxLog
import com.three.box.entity.BoxLogResult
import com.three.box.network.kt.ApiServiceFactory
import com.three.box.network.kt.AppApiService
import com.three.box.network.kt.AppHost
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class InOutLogActivity : BaseActivity() {

    companion object {
        const val TAG = "InOutLogActivity"
    }

    private lateinit var binding: ActivityInOutLogBinding

    private lateinit var logAdapter: InOutLogAdapter

    private var boxLogList = mutableListOf<BoxLog>()

    private var boxId = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityInOutLogBinding.inflate(layoutInflater)
        setStatusBar()
        super.onCreate(savedInstanceState)

        boxId = intent.getIntExtra("box_id", -1)

        initView()

        initValues()

        productHistory()
    }

    override fun getLayoutView() = binding.root

    private fun initView() {
        binding.backIv.setOnClickListener { finish() }
        logAdapter = InOutLogAdapter()
        with(binding.recyclerView) {
            layoutManager = LinearLayoutManager(context).apply { orientation = androidx.recyclerview.widget.RecyclerView.VERTICAL }
            addItemDecoration(DividerItemDecoration(this@InOutLogActivity, DividerItemDecoration.VERTICAL))
            adapter = logAdapter
        }
//        listAdapter.setOnItemClickListener { adapter, _, position ->
//            val group = adapter.getItem(position) as Group
//            callback?.invoke(group)
//            dismiss()
//        }
        logAdapter.submitList(boxLogList)
    }

    private fun initValues() {
//        productConfigList.addAll(AppRepository.instance.productConfigList)
//        adapter.notifyDataSetChanged()
    }

    inner class InOutLogAdapter : BaseQuickAdapter<BoxLog, QuickViewHolder>() {
        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            // 返回一个 ViewHolder
            return QuickViewHolder(R.layout.item_in_out_log, parent)
        }

        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: BoxLog?) {
            holder.setText(R.id.desc_tv, getString(R.string.format_log_product_desc, item?.productDesc, item?.productModelName))
            holder.setText(R.id.group_tv, getString(R.string.format_group, item?.groupName))
            holder.setText(R.id.operator_tv, getString(R.string.format_operate_name, item?.userInfo?.username))
            holder.setText(R.id.operator_type_tv, getString(R.string.format_operate_type, item?.operateTypeName))
        }
    }

    private val service = ApiServiceFactory.createAPIService(AppHost.AppApi, AppApiService::class.java)
    private fun productHistory() {
        val call = service.productHistory(boxId.toInt())
        call.enqueue(object : Callback<ApiResult<BoxLogResult>> {
            override fun onResponse(
                call: Call<ApiResult<BoxLogResult>>,
                response: Response<ApiResult<BoxLogResult>>
            ) {
                val apiResult = response.body()
                if (apiResult?.errno == ApiResult.SUCCESS) {
                    val list = apiResult.data.data
                    boxLogList.clear()
                    boxLogList.addAll(list)
                    logAdapter.addAll(boxLogList)
                } else {
                    Log.d(TAG, "error, ${apiResult?.errmsg}")
                }
            }

            override fun onFailure(call: Call<ApiResult<BoxLogResult>>, t: Throwable) {
//                Toaster.show("数据保存失败")
                t.printStackTrace()
            }
        })
    }
}